package com.fancyy.scm.activity;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.databinding.ObservableField;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.fancyy.scm.R;
import com.fancyy.scm.base.BaseActivity;
import com.fancyy.scm.base.annotation.Layout;
import com.fancyy.scm.base.annotation.Translucent;
import com.fancyy.scm.base.http.BRDataListener;
import com.fancyy.scm.base.http.BRDataTransport;
import com.fancyy.scm.bean.ErrorEnum;
import com.fancyy.scm.bean.HttpMessage;
import com.fancyy.scm.bean.ScanResult;
import com.fancyy.scm.databinding.DisanyeBinding;
import com.fancyy.scm.util.DialogUtil;
import com.google.gson.reflect.TypeToken;
import com.uuzuche.lib_zxing.activity.CaptureActivity;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import java.util.ArrayList;
import java.util.List;

import static com.fancyy.scm.BRConstant.SCANNING;
import static com.fancyy.scm.BRConstant.SCANNING_LOAD_ERROR;
import static com.fancyy.scm.BRConstant.SCANNING_SUBMIT;

@Translucent
@Layout(R.layout.disanye)
public class ScanningActivity extends BaseActivity<DisanyeBinding> {
    public ObservableField<String> shoeScanning = new ObservableField<>();
    public ObservableField<String> shoeModel = new ObservableField<>();
    public ObservableField<String> shoeSize = new ObservableField<>();
    public ObservableField<String> shoeColor = new ObservableField<>();

    public ObservableField<String> outScanning = new ObservableField<>();
    public ObservableField<String> outModel = new ObservableField<>();
    public ObservableField<String> outSize = new ObservableField<>();
    public ObservableField<String> outColor = new ObservableField<>();


    public void onShoesScanButtonClick(View view) {
        if(!isErrorPopUp)
            doScanning(1);
    }

    public void onOuterPackingScanButtonClick(View view) {
        if(!isErrorPopUp)
            doScanning(2);
    }

    private boolean isErrorPopUp=false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setNeedScan(true);
        super.onCreate(savedInstanceState);
    }

    int type;
    boolean scanShoe = false;
    boolean scanOutbox = false;

    private void doScanning(int type) {
        if(!getPDA()) {
            Intent intent = new Intent(this, CaptureActivity.class);
            startActivityForResult(intent, 1);
        }
    }

    @Override
    public void doScan(String data) {
        if(!isErrorPopUp)
            handleCode(data);
//        closeScan();
    }

    public int isMatch;
    public String remark;
    private void submit() {
        if (shoeScanning.get().equals(outScanning.get())) {
            isMatch = 1;
            playSound(R.raw.success);
            submitNet();
        } else {
            playSound(R.raw.error);
            isMatch = 0;
            isErrorPopUp = true;
            loadErrorCodes();
        }
    }

    private void submitNet() {
        BRDataTransport.create(SCANNING_SUBMIT)
                .addProgressing(self(), getString(R.string.Progressing))
                .addParam("ShoesBarCode", shoeScanning.get())
                .addParam("OuterBoxCode", outScanning.get())
                .addParam("CreateBy", getBRApplication().getUserInfo().userAccount)
                .addParam("IsMatch", isMatch)
                .addParam("Remark", remark)
                .execute(new BRDataListener<Object>() {
                    @Override
                    public void onMessage(HttpMessage<Object> message) {
                        super.onMessage(message);
                        scanShoe = false;
                        scanOutbox = false;
                    }

                    @Override
                    public void onSuccess(Object data) {
                        if (isMatch == 1) {
                            showToast("Scanning Data Match");
                        } else {
                            showToast("Submit Success");
                        }
                        shoeScanning.set(null);
                        shoeColor.set(null);
                        shoeSize.set(null);
                        shoeModel.set(null);
                        outScanning.set(null);
                        outColor.set(null);
                        outModel.set(null);
                        outSize.set(null);
                    }
                }, Object.class);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1 && resultCode == RESULT_OK) {
            String code = data.getStringExtra(CodeUtils.RESULT_STRING);
            handleCode(code);
        }
    }

    private void handleCode(String code) {
        if (shoeModel.get() == null) {
            type = 1;
            shoeScanning.set(code);
        } else {
            type = 0;
            outScanning.set(code);
        }
        BRDataTransport.createGet(SCANNING + "?BarCode=" + (type == 1 ? shoeScanning.get() : outScanning.get()))
                .addProgressing(self(), getString(R.string.Progressing))
                .execute(new BRDataListener<ScanResult>() {
                    @Override
                    public void onMessage(HttpMessage<ScanResult> message) {
                        if (type == 1) {
                            scanShoe = true;
                        } else {
                            scanOutbox = true;
                        }
                        super.onMessage(message);
                        if (scanShoe && scanOutbox) {
                            submit();
                        }
                    }

                    @Override
                    public void onSuccess(ScanResult data) {
                        if (type == 1) {
                            if (data != null) {
                                shoeModel.set(data.model);
                                shoeSize.set(data.size);
                                shoeColor.set(data.color);
                            }
                            scanShoe = true;
                        } else {
                            if (data != null) {
                                outModel.set(data.model);
                                outSize.set(data.size);
                                outColor.set(data.color);
                            }
                            scanOutbox = true;
                        }
                    }
                }, ScanResult.class);
    }

    List<ErrorEnum> errorData;
    private void loadErrorCodes() {
        BRDataTransport.createGet(SCANNING_LOAD_ERROR + "?EnumCategory=7")
                .addProgressing(self(), getString(R.string.Progressing))
                .execute(new BRDataListener<List<ErrorEnum>>() {
                    @Override
                    public void onSuccess(List<ErrorEnum> data) {
                        errorData = data;
                        getDataBinding().submit.setVisibility(View.VISIBLE);
                        TextView t = findViewById(R.id.code);
                        t.setText(data.get(0).enumName);
                        remark = data.get(0).enumCode;
                    }
                }, new TypeToken<List<ErrorEnum>>(){});
    }

    public void onCancelClick(View view) {
        remark = null;
        getDataBinding().submit.setVisibility(View.GONE);
        isErrorPopUp = false;
    }

    public void onErrorCodeClick(View view) {
        DialogUtil.showList(self(), "Error Item", errorData, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int which) {
                remark = errorData.get(which).enumCode;
                TextView t = findViewById(R.id.code);
                t.setText(errorData.get(which).enumName);
                dialogInterface.dismiss();
            }
        }, true);
    }

    public void onErrorSubmitClick(View view) {
        getDataBinding().submit.setVisibility(View.GONE);
        submitNet();
        isErrorPopUp = false;
    }
}
